extends "res://LevelManager.gd"

# 沙漠关卡特有属性
@export var sandstorm_chance: float = 0.1  # 沙尘暴触发几率
@export var sandstorm_duration: float = 10.0  # 沙尘暴持续时间
@export var sandstorm_damage: int = 1  # 沙尘暴每秒伤害
@export var oasis_chance: float = 0.05  # 绿洲生成几率
@export var oasis_healing: int = 5  # 绿洲每秒治疗量

# 沙尘暴计时器
var sandstorm_timer: float = 0.0
var is_sandstorm_active: bool = false
var sandstorm_active_timer: float = 0.0

# 绿洲引用
var oasis = null

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置关卡类型
	current_level_type = LevelType.DESERT
	
	# 根据难度调整参数
	adjust_difficulty()
	
	# 添加到关卡管理器组
	add_to_group("level_manager")
	
	# 设置背景
	var background_node = get_node_or_null("Background")
	if background_node:
		background_node.texture = load("res://asset/background/desert/image_fx_.jpg")
	
	# 初始化敌人生成器
	var enemy_spawner_node = get_node_or_null("EnhancedEnemySpawner")
	if enemy_spawner_node:
		enemy_spawner_node.level_type = LevelType.DESERT
		enemy_spawner_node.difficulty = level_difficulty
		enemy_spawner_node.start_spawning()
	
	# 显示关卡信息
	print("沙漠关卡 - 难度:", level_difficulty)

func _process(delta):
	# 调用父类的_process方法
	super._process(delta)
	
	# 处理沙尘暴逻辑
	if not is_sandstorm_active:
		sandstorm_timer += delta
		if sandstorm_timer >= 30.0:  # 每30秒检查一次
			sandstorm_timer = 0.0
			if randf() < sandstorm_chance:
				trigger_sandstorm()
	else:
		sandstorm_active_timer += delta
		
		# 沙尘暴伤害
		if int(sandstorm_active_timer) > int(sandstorm_active_timer - delta):  # 每秒触发一次
			if player and is_instance_valid(player):
				player.take_damage(sandstorm_damage)
				
				# 减速效果
				if player.has_method("apply_slow_effect"):
					player.apply_slow_effect(0.8, 1.5)
		
		# 沙尘暴结束
		if sandstorm_active_timer >= sandstorm_duration:
			end_sandstorm()
	
	# 处理绿洲逻辑
	if not oasis and randf() < oasis_chance * delta:
		spawn_oasis()

func adjust_difficulty():
	# 根据难度调整参数
	match level_difficulty:
		1:
			level_time_limit = 180.0  # 3分钟
			sandstorm_chance = 0.05
			sandstorm_damage = 1
		2:
			level_time_limit = 210.0  # 3.5分钟
			sandstorm_chance = 0.1
			sandstorm_damage = 1
		3:
			level_time_limit = 240.0  # 4分钟
			sandstorm_chance = 0.15
			sandstorm_damage = 2
		4:
			level_time_limit = 270.0  # 4.5分钟
			sandstorm_chance = 0.2
			sandstorm_damage = 2
		5:
			level_time_limit = 300.0  # 5分钟
			sandstorm_chance = 0.25
			sandstorm_damage = 3

func trigger_sandstorm():
	is_sandstorm_active = true
	sandstorm_active_timer = 0.0
	
	# 创建沙尘暴效果
	var sandstorm = preload("res://SandstormEffect.tscn").instantiate()
	add_child(sandstorm)
	sandstorm.global_position = Vector2(get_viewport_rect().size.x / 2, get_viewport_rect().size.y / 2)
	sandstorm.duration = sandstorm_duration
	
	# 通知UI
	if ui:
		ui.show_message("沙尘暴来袭！")
	
	print("沙尘暴开始!")

func end_sandstorm():
	is_sandstorm_active = false
	
	# 通知UI
	if ui:
		ui.show_message("沙尘暴结束")
	
	print("沙尘暴结束!")

func spawn_oasis():
	# 创建绿洲
	oasis = preload("res://OasisEffect.tscn").instantiate()
	add_child(oasis)
	
	# 设置绿洲位置（随机位置，但不要太靠近边缘）
	var viewport_size = get_viewport_rect().size
	var x = randf_range(100, viewport_size.x - 100)
	var y = randf_range(100, viewport_size.y - 100)
	oasis.global_position = Vector2(x, y)
	
	# 设置绿洲属性
	oasis.healing_amount = oasis_healing
	oasis.duration = 20.0  # 绿洲持续20秒
	
	# 通知UI
	if ui:
		ui.show_message("发现绿洲！")
	
	print("绿洲出现!")
	
	# 设置绿洲消失计时器
	var timer = get_tree().create_timer(20.0)
	timer.connect("timeout", _on_oasis_timeout)

func _on_oasis_timeout():
	if oasis and is_instance_valid(oasis):
		oasis.queue_free()
		oasis = null
		
		# 通知UI
		if ui:
			ui.show_message("绿洲消失了")
		
		print("绿洲消失!")

# 重写完成关卡方法，添加沙漠特有的完成条件
func complete_level():
	# 检查是否击败了足够的敌人
	var enemy_spawner_node = get_node_or_null("EnhancedEnemySpawner")
	if enemy_spawner_node:
		var enemies_defeated = enemy_spawner_node.enemies_defeated
		var required_defeats = 50 + (level_difficulty * 10)  # 根据难度调整所需击败敌人数
		
		if enemies_defeated >= required_defeats:
			# 调用父类的完成方法
			super.complete_level()
		elif level_timer >= level_time_limit:
			# 时间到但没有击败足够的敌人
			fail_level()
	else:
		# 如果没有敌人生成器，使用默认完成逻辑
		super.complete_level()
